function Course_Self_list() {
    var _this = this;
    var page;
    _this.obj = null;
    _this.templateArr = [];
    _this.page_size = 10;
    var departmentId = $("#departmentId").val();
    var roleName = $("#user-role-name").val();
    // init
    _this.init = function (_page) {
        page = _page;
        course_type.iniTree(appPath + '/admin/exCourseTypeSelf/getTree');
        var treeObj = $.fn.zTree.getZTreeObj("course_treeId");
        var node = treeObj.getSelectedNodes();
        $("#intTypeId").val(node[0].id);
        var value = $('#click_course_status span[class="active"]').attr("data-value");
        _this.changeList(value);
        _this.initEvent();
    }

    _this.initTable = function () {
        var list_url = appPath + "/admin/course/course_list";
        page.init("select_course_form", list_url, "select_course_list", "select_course_page", 1, _this.page_size);
        page.goPage(1);
        page.list = function (dataList) {
            _this.obj = dataList;
            if (dataList == null) {
                return;
            }
            var len = dataList.length;
            var inner = "", item;
            // 组装数据
            for (var i = 0; i < len; i++) {
                item = dataList[i];
                inner += '<tr >';
                inner += '<td width="50">' + (i + 1) + '</td>';
                inner += '<td width="50"><input type=\"checkbox\" name="courseBox" value="' + item['course_id'] + '"  data-id="' + item['course_id'] + '"></td>';
                inner += '<td width="100">' + item['course_no'] + '</td>';
                inner += '<td width="100"><span class="text-orange tooltip" data-length="15">' + item['course_name'] + '</span></td>';
                inner += '<td width="70">' + item['class_hour'] + '</td>';
                inner += '<td width="70">' + item['question_count'] + '</td>';
                inner += '<td width="360">';

                //以下是部门管理员
                if (roleName === "department_admin") {
                    //判断用户的部门ID 是否和课程的ID一样
                    if (item['departmentId'] === departmentId) {
                        inner += '<a href="javascript:course_self_list.info(\'' + item['course_id'] + '\');" class="a a-view">详情</a> ';
                        inner += '<a href="javascript:course_self_list.edit(\'' + item['course_id'] + '\');" class="a a-info">编辑</a> ';
                        inner += '<a href="javascript:course_self_list.attachmentManage(\'' + item['course_id'] + '\',\'' + 3 + '\');" class="a a-info">课件管理</a> ';
                        inner += '<a href="javascript:course_self_list.questionManage(\'' + item['course_id'] + '\',\'' + 3 + '\');" class="a a-publish">题库管理</a> ';
                        inner += '<a href="javascript:course_self_list.courseView(\'' + item['course_id'] + '\');" class="a a-info">课件预览</a> ';
                        inner += '<a href="javascript:course_self_list.questionView(\'' + item['course_id'] + '\');" class="a a-publish">题库预览</a>';
                    }else {
                        inner += '<a href="javascript:course_self_list.info(\'' + item['course_id'] + '\');" class="a a-view">详情</a> ';
                        inner += '<a href="javascript:;" class="a" disabled="disabled">编辑</a> ';
                        inner += '<a href="javascript:;" class="a" disabled="disabled">课件管理</a> ';
                        inner += '<a href="javascript:;" class="a" disabled="disabled">题库管理</a> ';
                        inner += '<a href="javascript:course_self_list.courseView(\'' + item['course_id'] + '\');" class="a a-info">课件预览</a> ';
                        inner += '<a href="javascript:course_self_list.questionView(\'' + item['course_id'] + '\');" class="a a-publish">题库预览</a>';
                    }
                }
                //以下是单位管理员
                else {
                    inner += '<a href="javascript:course_self_list.info(\'' + item['course_id'] + '\');" class="a a-view">详情</a> ';
                    inner += '<a href="javascript:course_self_list.edit(\'' + item['course_id'] + '\');" class="a a-info">编辑</a> ';
                    inner += '<a href="javascript:course_self_list.deleteCourse(\'' + item['course_id'] + '\');" class="a a-close">删除</a> ';
                    inner += '<a href="javascript:course_self_list.attachmentManage(\'' + item['course_id'] + '\',\'' + 3 + '\');" class="a a-info">课件管理</a> ';
                    inner += '<a href="javascript:course_self_list.questionManage(\'' + item['course_id'] + '\',\'' + 3 + '\');" class="a a-publish">题库管理</a> ';
                    inner += '<a href="javascript:course_self_list.courseView(\'' + item['course_id'] + '\');" class="a a-info">课件预览</a> ';
                    inner += '<a href="javascript:course_self_list.questionView(\'' + item['course_id'] + '\');" class="a a-publish">题库预览</a>';
                }
                inner += '</td></tr>';
            }
            return inner;
        }

    };

    _this.initCourseSelfTable = function () {
        var list_url = appPath + "/admin/exCourseSelf/course_self_list";
        page.init("select_course_form", list_url, "select_course_self_list", "select_course_self_page", 1, _this.page_size);
        page.goPage(1);
        page.list = function (dataList) {
            _this.obj = dataList;
            if (dataList == null) {
                return;
            }
            var len = dataList.length;
            var inner = "", item;
            // 组装数据
            for (var i = 0; i < len; i++) {
                item = dataList[i];
                inner += '<tr >';
                inner += '<td width="45">' + (i + 1) + '</td>';
                inner += '<td width="40"><input type=\"checkbox\" name="courseBox" value="' + item['intId'] + '" data-id="' + item['intId'] + '"></td>';
                inner += '<td width="148">' + item['varCode'] + '</td>';
                inner += '<td width="110"><span class="text-orange tooltip" data-length="15">' + item['varName'] + '</span></td>';
                inner += '<td width="60">' + item['intClassHour'] + '</td>';
                inner += '<td width="60">' + item['attachmentCount'] + '</td>';
                inner += '<td width="60">' + item['questionCount'] + '</td>';

                if (item['chrStatus'] == '1') {
                    inner += '<td width="60"><a class="btn btn-default" data-toggle="modal"  onclick="course_self_list.checkStatus(' + item['intId'] + ')">查看状态</a></td>';
                } else {
                    inner += '<td width="60">' + course_self_list.getChrStatusName(item['chrStatus']) + '</td>';
                }

                inner += '<td width="342">';
                //以下是部门管理员
                if (roleName === "department_admin") {
                    //判断用户的部门ID 是否和课程的ID一样
                    if (item['departmentId'] === departmentId) {
                        inner += '<a href="javascript:course_self_list.edit(\'' + item['intId'] + '\');" class="a a-info">编辑</a> ';
                        inner += '<a href="javascript:course_self_list.delete(\'' + item['intId'] + '\',\'' + item['chrStatus'] + '\');" class="a a-close">删除</a> ';
                        inner += '<a href="javascript:course_self_list.attachmentManage(\'' + item['intId'] + '\',\'' + item['chrStatus'] + '\');" class="a a-info">课件管理</a> ';
                        inner += '<a href="javascript:course_self_list.questionManage(\'' + item['intId'] + '\',\'' + item['chrStatus'] + '\');" class="a a-publish">题库管理</a> ';
                        if ("2" == item['chrStatus']) {
                            inner += '<a href="javascript:course_self_list.submitExCourseSelf(\'' + item['intId'] + '\');" class="a a-view">提交</a> ';
                        }
                    } else {
                        inner += '<a href="javascript:;" class="a" disabled="disabled">编辑</a> ';
                        inner += '<a href="javascript:;" class="a" disabled="disabled">删除</a> ';
                        inner += '<a href="javascript:;" class="a" disabled="disabled">课件管理</a> ';
                        inner += '<a href="javascript:;" class="a" disabled="disabled">题库管理</a> ';
                        if ("2" == item['chrStatus']) {
                            inner += '<a href="javascript:;" class="a" disabled="disabled">提交</a> ';
                        }
                    }
                }
                //以下是单位管理员
                else {
                    inner += '<a href="javascript:course_self_list.edit(\'' + item['intId'] + '\');" class="a a-info">编辑</a> ';
                    inner += '<a href="javascript:course_self_list.delete(\'' + item['intId'] + '\',\'' + item['chrStatus'] + '\');" class="a a-close">删除</a> ';
                    inner += '<a href="javascript:course_self_list.attachmentManage(\'' + item['intId'] + '\',\'' + item['chrStatus'] + '\');" class="a a-info">课件管理</a> ';
                    inner += '<a href="javascript:course_self_list.questionManage(\'' + item['intId'] + '\',\'' + item['chrStatus'] + '\');" class="a a-publish">题库管理</a> ';
                    inner += '<a href="javascript:course_self_list.courseView(\'' + item['intId'] + '\',\'' + 1 + '\');" class="a a-info">课件预览</a> ';
                    inner += '<a href="javascript:course_self_list.questionView(\'' + item['intId'] + '\',\'' + 1 + '\');" class="a a-publish">题库预览</a>';
                    if ("5" == item['chrStatus']) {
                        inner += '<a href="javascript:course_self_list.auditExCourseSelf(\'' + item['intId'] + '\');" class="a a-view">审核</a> ';
                    }
                    if ("2" == item['chrStatus'] && item['departmentId'] == "") {
                        inner += '<a href="javascript:course_self_list.submitExCourseSelf(\'' + item['intId'] + '\');" class="a a-view">提交</a> ';
                    }
                }
                inner += '</td></tr>';
            }
            return inner;
        }

    };

    _this.getChrStatusName = function (data) {
        if (data == "0") {
            return '待上传';
        } else if (data == "1") {
            return '转码中';
        } else if (data == "2") {
            return '待提交';
        } else if (data == "3") {
            return '审核通过';
        } else if (data == "4") {
            return '审核不通过';
        } else if (data == "5") {
            return '待审核';
        }
    }

    _this.initEvent = function () {
// 确定
        $("#btn_sure").click(function () {

            if (_this.templateArr.length > 0) {

                parent.createPrivateProject.submitCourse(_this.templateArr);
            }
            _this.closeDialog();
        });

//取消
        $("#btn_cancel").click(function () {
            _this.closeDialog();
        });

//搜索
        $("#courseName_search").click(function () {
            _this.search();
        });
//全部
        $("#course_all").click(function () {
            _this.all();
        });

//课程新增
        $("#course_add").click(function () {
            var treeObj = $.fn.zTree.getZTreeObj("course_treeId");
            var node = treeObj.getSelectedNodes();
            if (node[0].level == 0) {
                layer.alert('根节点不能新增课程!', {icon: 2});
                return;
            }
            _this.courseAdd(node[0].id);
        });

//课程移动
        $("#course_move").click(function () {
            _this.courseMove();
        });

//课程预览
        $("#course_view").click(function () {
            _this.courseView();
        });
//题库预览
        $("#question_view").click(function () {
            _this.questionView();
        });

//全选
        $("#checkAll").click(function () {
            _this.courseCheckAll();
        });
//全选
        $("#checkAllSelf").click(function () {
            _this.courseSelfCheckAll();
        });

//错题反馈
        $("#question_back").on("click", function () {
            _this.questionBack();
        });

    }

//关闭对话框
    _this.closeDialog = function () {
        parent.layer.close(parent.layer.getFrameIndex(window.name));
    }
//获取id索引
    _this.getIndex = function (obj, attr) {
        var index = -1;
        $.each(obj, function (i, n) {
            if (this.intId == attr) {
                index = i;
                return false;
            }
        })
        return index;
    }

//搜索
    _this.search = function () {
        $("#courseName").val($.trim($("#courseName").val()));
        var value = $('#click_course_status span[class="active"]').attr("data-value");
        _this.changeList(value);
    };
//全部
    _this.all = function () {
        var treeObj = $.fn.zTree.getZTreeObj("course_treeId");
        var node = treeObj.getSelectedNodes();
        $("#intTypeId").val(node[0].id);
        $("#courseName").val("");
        var value = $('#click_course_status span[class="active"]').attr("data-value");
        _this.changeList(value);
    };
//详情
    _this.info = function (courseId) {
        var url = appPath + "/admin/course/course_info?courseId=" + courseId + "&selfFlag=1";
        window.open(url, "_blank");
    }
//课程预览
    _this.courseView = function (courseId,type) {
        common.goto_student_course_view(courseId,type);
    }
//题库预览
    _this.questionView = function (courseId,type) {
        common.goto_student_course_question_view(courseId,type);

    }

    _this.courseAdd = function (currCourseTypeId) {
        var companyId = $("#companyId").val();
        var departmentId = $("#departmentId").val();
        window.location.href = appPath + '/admin/exCourseSelf/courseSelfAdd?courseTypeId=' + currCourseTypeId + '&companyId=' + companyId + '&departmentId=' + departmentId;
    };
    _this.edit = function (courseId) {
        window.location.href = appPath + '/admin/exCourseSelf/courseSelfAdd?courseId=' + courseId;
    };

    _this.deleteCourse = function (courseId) {
        layer.confirm('确定要删除选择的数据吗？', {btn: ['是', '否']}, function (index) {
            layer.close(index);
            $.ajax({
                url: appPath + "/admin/exCourseSelf/deleteCourse",
                data: {
                    "courseIds": courseId,
                    "companyId": $("#companyId").val()
                },
                dataType: "json",
                async: true,
                type: "POST",
                success: function (data) {
                    var result = eval(data).result;
                    if (result == "success") {
                        layer.msg("删除成功", {icon: 1});

                        // 触发查询事件
                        $("#courseName_search").trigger("click");
                    } else {
                        var tipMsg = "删除失败!";
                        var error = eval(data).errorMsg;
                        if (error != null && error != "") {
                            tipMsg += "\n" + error;
                        }
                        layer.msg(tipMsg, {icon: 2});
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg(textStatus, {icon: 2});
                }
            });
        }, function () {

        });
    }

    _this.delete = function (courseId, chrStatus) {
        var courses = [];
        var object = new Object();
        object.courseId = courseId;
        object.chrStatus = chrStatus;
        courses.push(object);
        courses = JSON.stringify(courses);

        layer.confirm('确定要删除选择的数据吗？', {btn: ['是', '否']}, function (index) {
//删除
            layer.close(index);

            $.ajax({
                url: appPath + "/admin/exCourseSelf/delete",
                data: {
                    "courseIds": courses,
                    "companyId": $("#companyId").val()
                },
                dataType: "json",
                async: true,
                type: "POST",
                success: function (data) {
                    var result = eval(data).result;
                    var count = eval(data).count;
                    if (result == "success") {
                        if ("0" == count) {
                            layer.msg("删除成功", {icon: 1});
                        } else {
                            var tipMsg = count + "个课程删除失败!";
                            tipMsg += "\n 该课程下可能有附件或试题，如要删除请先删除课件或试题，若该课程已被项目引用，不允许删除";
                            layer.msg(tipMsg, {icon: 5});
                        }

                        // 触发查询事件
                        $("#courseName_search").trigger("click");
                    } else {
                        var tipMsg = "删除失败!";
                        var error = eval(data).errorMsg;
                        if (error != null && error != "") {
                            tipMsg += "\n" + error;
                        }
                        layer.msg(tipMsg, {icon: 2});
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    layer.msg(textStatus, {icon: 2});
                }
            });
        }, function () {

        });
    };
// 课件管理
    _this.attachmentManage = function (courseId, chrStatus) {
        var url = appPath + "/admin/attachmentSelf/index?courseId=" + courseId + "&chrStatus=" + chrStatus;
        window.open(url, "_blank");
    };
    _this.questionManage = function (courseId, chrStatus) {
        var url = appPath + "/admin/questionSelf/question?courseId=" + courseId + "&companyId=" + $("#companyId").val() + "&chrStatus=" + chrStatus;
        window.open(url, "_blank");
    };

    _this.auditExCourseSelf = function (id) {
        layer.confirm('审核是否通过？', {btn: ['是', '否']}, function (index) {
            $.ajax({
                url: appPath + "/admin/exCourseSelf/audit",
                data: {"id": id},
                dataType: "json",
                type: "GET",
                success: function (data) {
                    var code = data.code;
                    if ("10000" == code) {
                        layer.msg("保存成功");
                    } else {
                        var tipMsg = "保存失败!";
                        layer.msg(tipMsg);
                    }
                    _this.initCourseSelfTable();
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {

                }
            });
        }, function () {
            $.ajax({
                url: appPath + "/admin/exCourseSelf/auditNo",
                data: {"id": id},
                dataType: "json",
                type: "GET",
                success: function (data) {
                    var code = data.code;
                    if ("10000" == code) {
                        layer.msg("保存成功");
                    } else {
                        var tipMsg = "保存失败!";
                        layer.msg(tipMsg);
                    }
                    _this.initCourseSelfTable();
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {

                }
            });
        });

    }

//提交
    _this.submitExCourseSelf = function (id) {
        $.ajax({
            url: appPath + "/admin/exCourseSelf/submit",
            data: {"id": id},
            dataType: "json",
            type: "GET",
            success: function (data) {
                var code = data.code;
                if ("10000" == code) {
                    layer.msg("保存成功");
                } else {
                    var tipMsg = "保存失败!";
                    layer.msg(tipMsg);
                }
                _this.initCourseSelfTable();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            }
        });
    }

//课程移动
    _this.courseMove = function () {
        var courseIds = [];
        $("#select_course_list input:checked").each(function (i, e) {
            courseIds.push($(this).attr("data-id"))
        });
        if (courseIds.length == 0) {
            layer.alert('请至少选择一条记录!', {icon: 2});
            return;
        }
        var html = '';
        html += '<div style="padding:0px 30px;">';
        html += '<div style="padding: 10px 0px;"><input id="searchbox" class="easyui-searchbox"  placeholder="请输入名称自动检索" style="width:270px;height:30px;"/></div>';
        html += '<div style="padding: 0px 10px;"><ul id="courseType_tree" class="ztree"></ul></div>';
        html += '</div>';
        layer.open({
            type: 1,
            title: '移动课程',
            shadeClose: true,
            scrollbar: false,
            area: ['350px', '80%'],
            content: html
        });
        _this.Movetree.iniTree();
    }

//课程全选
    _this.courseCheckAll = function () {
        var flag = $("#checkAll").prop("checked");
        $('#select_course_self_list input[type="checkbox"]').prop("checked", flag);
    }
//课程全选
    _this.courseSelfCheckAll = function () {
        var flag = $("#checkAllSelf").prop("checked");
        $('#select_course_list input[type="checkbox"]').prop("checked", flag);
    }


// 移动树
    _this.Movetree = {
        setting: {
            view: {
                dblClickExpand: false,
                fontCss: getFontCss
            },
            callback: {
                onClick: function (e, treeId, treeNode) {
                    _this.Movetree.onClick(treeNode);
                }
            }
        },
        onClick: function (treeNode) {
            if (treeNode.level == 0) {
                layer.msg("不能移动到根节点", {icon: 2});
                return;
            }
            layer.confirm('您确定要移动吗？', {
                icon: 3,
                btn: ['移动', '取消']
                // 按钮
            }, function () {
                var courseIds = [];
                $("#select_course_list input:checked").each(function (i, e) {
                    courseIds.push($(this).attr("data-id"));
                });
                _this.move(courseIds, treeNode.id);
            });
        },
        iniTree: function () {
            var chrSource = $("#chrSource").val() == undefined ? "" : $("#chrSource").val();
            var companyId = $("#companyId").val();
            var url = appPath + '/admin/exCourseTypeSelf/getTree?chrSource=' + chrSource + '&companyId=' + companyId;
            var result = _this.ajax(url);
            var zNodes = eval(result);
            $.fn.zTree.init($("#courseType_tree"), _this.Movetree.setting, zNodes);
        }
    };

// 执行移动
    _this.move = function (courseIds, courseTypeId) {
        var param = {
            "courseIds": courseIds.join(","),
            "courseTypeId": courseTypeId
        };
        var url = appPath + "/admin/course/course_move";
        var result = _this.ajax(url, param);
        if ("10000" == result) {
            layer.alert('操作成功', {
                icon: 1
            }, function (index) {
                layer.closeAll();
                _this.search();
                $("#checkAll").prop("checked", false);
                return true;
            });
        } else {
            layer.alert('操作失败', {
                icon: 2
            });
        }
    };


    _this.ajax = function (url, param, type) {
        var result;
        $.ajax({
            url: url,
            async: false,
            type: 'post',
            data: param,
            success: function (data) {
                result = data.result;
            }
        });
        return result;
    };

    _this.changeList = function (value) {
// 已审核
        if ("0" == value) {
            course_self_list.initTable();
        }
// 未审核
        if ("1" == value) {
            course_self_list.initCourseSelfTable();
        }
    };

//错题反馈
    _this.questionBack = function () {
        var url = appPath + "/feedback/self";
        window.open(url, "_blank");
    }

// 查看转码状态
    _this.checkStatus = function (courseId) {
        $.ajax({
            url: appPath + "/admin/exCourseSelf/isTranscoding",
            data: {
                "courseId": courseId
            },
            dataType: "json",
            async: true,
            type: "GET",
            success: function (data) {
                var result = eval(data).result;
                console.log(result);
                if (result == "success") {
                    layer.msg("转码已完成", {icon: 1});
                    course_self_list.initCourseSelfTable();
                } else if (result == "fail") {
                    var failList = eval(data).failList;
                    var ingList = eval(data).ingList;
                    console.log(failList);
                    console.log(ingList);
                    //转码失败, 反馈失败的课件名
                    var htm = '<div><table width="500"  border="1">';
                    htm += '<tr><th>课件名称</th>';
                    htm += '<th>状态</th></tr>';
                    if(failList.length > 0){
                        for(var i = 0; i< failList.length; i++){
                            htm += '<tr style="color: red;"><td>'+failList[i]+'</td>';
                            htm += '<td>转码失败</td></tr>';
                        }
                    }
                    if(ingList.length > 0){
                        for(var j = 0; j< ingList.length; j++){
                            htm += '<tr><td>'+ingList[j]+'</td>';
                            htm += '<td>转码中...</td></tr>';
                        }

                    }
                    htm += '</table></div>';
                    layer.open({
                        type: 1,
                        title: '课件状态', //样式类名
                        closeBtn: 1, //不显示关闭按钮
                        anim: 2,
                        area: ['500px', '215px'],
                        shadeClose: true, //开启遮罩关闭
                        content: htm
                    });

                } else if (result == "error") {
                    //接口报错
                    layer.msg("系统异常,请联系管理员!", {icon: 7});
                }
            }
        });
    }
}

var course_self_list = new Course_Self_list();


//搜索高亮
function getFontCss(treeId, treeNode) {
    return (!!treeNode.highlight) ? {'color': "#A60000", 'font-weight': "bold"} : {
        'color': "#333",
        'font-weight': "normal"
    };
}